using System;
using System.ComponentModel;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;

[assembly: WebResource("Ratman.SharePoint.SharePointSandBoxMediaPlayer.TEMPLATE.FEATURES.SharePointSandBoxMediaPlayerResources.JS.MediaPlayer.js", "application/x-javascript")]
[assembly: WebResource("Ratman.SharePoint.SharePointSandBoxMediaPlayer.TEMPLATE.FEATURES.SharePointSandBoxMediaPlayerResources.JS.Silverlight.js", "application/x-javascript")]
[assembly: WebResource("Ratman.SharePoint.SharePointSandBoxMediaPlayer.TEMPLATE.FEATURES.SharePointSandBoxMediaPlayerResources.XAML.MediaPlayer.xaml", "application/xaml+xml")]

namespace Ratman.SharePoint.SharePointSandBoxMediaPlayer
{
    public class SharePointSandBoxMediaPlayerWebPart : WebPart
    {
        public SharePointSandBoxMediaPlayerWebPart()
        {
            this.ExportMode = WebPartExportMode.None;
            this.Height = Unit.Pixel(300);
        }

        const string LibraryBasePath = "/SharePointSandBoxMediaPlayerResources";
        const string ApplicationResourcesBasePath = "/Resources/SharePointSandBoxMediaPlayerResources";
        
        public enum ResourceMethod
        {
            Library,
            ApplicationResource,
            Embedded,
        }

        private const ResourceMethod c_ResourceAccessMethodDefault = ResourceMethod.Library;
        private ResourceMethod m_ResourceAccessMethod = c_ResourceAccessMethodDefault;

        [Personalizable]
        [WebBrowsable]
        [Category("Media Player Settings")]
        [DefaultValue(c_ResourceAccessMethodDefault)]
        [WebDisplayName("Resource Method")]
        [WebDescription("The method used to access the web part resources.")]
        public ResourceMethod ResourceAccessMethod
        {
            get { return m_ResourceAccessMethod; }
            set { m_ResourceAccessMethod = value; }
        }

        private const string c_VideoUrlDefault = "";
        private string m_VideoUrl = c_VideoUrlDefault;

        [Personalizable]
        [WebBrowsable]
        [Category("Media Player Settings")]
        [DefaultValue(c_VideoUrlDefault)]
        [WebDisplayName("Video Source URL")]
        [WebDescription("The site relative URL to a video source to be displayed in the SharePoint sandBox Media Player.")]
        public string VideoUrl
        {
            get { return m_VideoUrl; }
            set { m_VideoUrl = value; }
        }

        private const string c_BackgroundColorDefault = "#FFCCCCCC";    //silver
        private string m_BackgroundColor = c_BackgroundColorDefault;

        [Personalizable]
        [WebBrowsable]
        [Category("Media Player Settings")]
        [DefaultValue(c_BackgroundColorDefault)]
        [WebDisplayName("Background Color")]
        [WebDescription("The background color of the SharePoint sandBox Media Player.")]
        public string BackgroundColor
        {
            get { return m_BackgroundColor; }
            set { m_BackgroundColor = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            switch (ResourceAccessMethod)
            {
                case ResourceMethod.Library:
                    Page.ClientScript.RegisterClientScriptInclude("Library.Silverlight.js", LibraryBasePath + "/JS/Silverlight.js");
                    Page.ClientScript.RegisterClientScriptInclude("Library.MediaPlayer.js", LibraryBasePath + "/JS/MediaPlayer.js");
                    break;
                case ResourceMethod.ApplicationResource:
                    Page.ClientScript.RegisterClientScriptInclude("ApplicationResource.Silverlight.js", ApplicationResourcesBasePath + "/JS/Silverlight.js");
                    Page.ClientScript.RegisterClientScriptInclude("ApplicationResource.MediaPlayer.js", ApplicationResourcesBasePath + "/JS/MediaPlayer.js");
                    break;
                case ResourceMethod.Embedded:
                    Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Ratman.SharePoint.SharePointSandBoxMediaPlayer.TEMPLATE.FEATURES.SharePointSandBoxMediaPlayerResources.JS.Silverlight.js");
                    Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Ratman.SharePoint.SharePointSandBoxMediaPlayer.TEMPLATE.FEATURES.SharePointSandBoxMediaPlayerResources.JS.MediaPlayer.js");
                    break;
            }

            if (_provider != null)
            {
                _provider.GetFieldValue(new FieldCallback(GetFieldValue));
            }

            base.OnPreRender(e);
        }

        static int sl_id = 0;

        protected override void RenderContents(HtmlTextWriter writer)
        {
            string videoLocation = ConnectionSuppliedValue();
            if (videoLocation == String.Empty)
                videoLocation = m_VideoUrl;

            StringBuilder content = new StringBuilder();

            string divID = "SLVideo_" + (sl_id++).ToString();
            content.Append("<div style=\"width:100%;height:100%\" id=\"" + divID + "\">");
            content.Append("</div>");

            content.Append("<script type=\"text/javascript\">");
            string xamlPath = "";
            switch (ResourceAccessMethod)
            {
                case ResourceMethod.Library:
                    xamlPath = LibraryBasePath + "/XAML/MediaPlayer.xaml";
                    break;
                case ResourceMethod.ApplicationResource:
                    xamlPath = ApplicationResourcesBasePath + "/XAML/MediaPlayer.xaml";
                    break;
                case ResourceMethod.Embedded:
                    xamlPath = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Ratman.SharePoint.SharePointSandBoxMediaPlayer.TEMPLATE.FEATURES.SharePointSandBoxMediaPlayerResources.XAML.MediaPlayer.xaml");
                    break;
            }
            content.Append("SandBox.StartPlayer('" + divID + "', '" + xamlPath + "', '" + videoLocation.ToString() + "', '" + BackgroundColor + "')");
            content.Append("</script>");

            writer.Write(content.ToString());
        }

        #region Connection Support

        private IWebPartField _provider;
        private object _fieldValue;

        private void GetFieldValue(object fieldValue)
        {
            _fieldValue = fieldValue;
        }

        public bool ConnectionPointEnabled
        {
            get
            {
                object o = ViewState["ConnectionPointEnabled"];
                return (o != null) ? (bool)o : true;
            }
            set
            {
                ViewState["ConnectionPointEnabled"] = value;
            }
        }

        private string ConnectionSuppliedValue()
        {
            if (_provider != null)
            {
                PropertyDescriptor prop = _provider.Schema;

                if (prop != null && _fieldValue != null)
                {
                    return _fieldValue.ToString();
                }
            }
            return String.Empty;
        }

        [ConnectionConsumer("Video Url", "SharePointSandBoxMediaPlayerConnectionPoint1", typeof(FieldConsumerConnectionPoint), AllowsMultipleConnections = false)]
        public void SetConnectionInterface(IWebPartField provider)
        {
            _provider = provider;
        }

        public class FieldConsumerConnectionPoint : ConsumerConnectionPoint
        {
            public FieldConsumerConnectionPoint(MethodInfo callbackMethod,
              Type interfaceType, Type controlType, string name, string id,
              bool allowsMultipleConnections)
                : base(
              callbackMethod, interfaceType, controlType,
              name, id, allowsMultipleConnections)
            {
            }

            public override bool GetEnabled(Control control)
            {
                return ((SharePointSandBoxMediaPlayerWebPart)control).ConnectionPointEnabled;
            }
        }

        #endregion

    }
}